from django.shortcuts import render, redirect
from XinYuWIT.models import TreeHoleEntry, User
from django import forms

class TreeHoleForm(forms.ModelForm):
    class Meta:
        model = TreeHoleEntry
        fields = ['mood', 'content']
        widgets = {
            'mood': forms.RadioSelect(attrs={
                'class': 'mood-radio',
                'style': 'display: none;'  # 隐藏原生radio
            }),
            'content': forms.Textarea(attrs={
                'class': 'post-content-input',
                'placeholder': '写下你想说的话...（最多500字）',
                'rows': 5,
                'maxlength': '500'
            })
        }
        labels = {
            'mood': '',
            'content': ''
        }


def tree_hole(request):
    if request.method == 'POST':
        form = TreeHoleForm(request.POST)
        info = request.session.get('info')
        user = User.objects.get(id=info.get('id'))
        if user.is_authenticated:
            if form.is_valid():
                new_entry = form.save(commit=False)
                new_entry.user = user

                new_entry.save()
                return redirect('/treehole/')
    else:
        form = TreeHoleForm()

    entries = TreeHoleEntry.objects.all()
    return render(request, 'treehole.html', {
        'form': form,
        'entries': entries
    })